package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FoodDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Food;
import com.itheima.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = FoodService.class)
@Transactional
public class FoodServiceImpl implements FoodService {

    @Autowired
    private FoodDao foodDao;

    @Override
    public void add(Food food) {
        foodDao.add(food);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //每页记录数
        Integer pageSize = queryPageBean.getPageSize();
        //查询条件
        String queryString = queryPageBean.getQueryString();
        //1.分页查询助手查询,基于mybatis框架提供的分页助手,省去sql中limit页码,条数;
        PageHelper.startPage(currentPage, pageSize);
        //2.获取带分页数据对象
        Page<Food> page = foodDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Food> rows = page.getResult();//pageSize条数据:10条
        return new PageResult(total, rows);
    }

    @Override
    public void deleteById(Integer id) {
        foodDao.deleteById(id);
    }

    @Override
    public void edit(Food food) {
        foodDao.edit(food);
    }

    @Override
    public Food findById(Integer id) {
        return foodDao.findById(id);
    }

    @Override
    public List<Food> findAll() {
        return foodDao.findAll();
    }

    @Override
    public void addList(List<Food> data) {
        for (Food food : data) {
            foodDao.add(food);
        }
    }
}
